<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2017/11/30
 * Time: 11:47
 */
require_once "AbstractUpload.php";
require_once "FileConfigException.php";
class SingleUpload extends AbstractUpload
{

    public function __construct(array $config)
    {
        parent::__construct($config);
    }
    public   function getFileDataInfo()
    {
        return [
            "file_path"=>$this->fileUrl[0],
            "size"=>$this->size[0],
            "finalFileName"=>$this->finalFileName[0]
        ];
    }
    public  function doupload()
    {
        $this->init();
        $uploadPath = $this->uploadPath."/".$this->getSavePathSuffix();
        if(!$this->mkdir($uploadPath)){
            throw new FileUploadException("创建目录【{$uploadPath}】失败");
        }
        $ext = $this->getFileExt($this->files[0]);
        //获取redis唯一id
        $index = $this->redis->incrby(self::UPLOAD_INDEX,1);
        $finalFileName = md5($this->getSaveFileKey()."_".$index).".".$ext;
        $destination = $uploadPath."/". $finalFileName;
        $this->finalFileName[] = $finalFileName;
        $this->fileUrl[] = rtrim($this->saveHttp,"/")."/".$this->getSavePathSuffix()."/".$finalFileName;
        if($this->moveFileToSavePath($this->files[0]["tmp_name"],$destination) === false){
            throw new FileUploadException("移动文件失败");
        }
        return true;
    }
}